package mainBoerse;

import java.util.Properties;
import java.util.ResourceBundle;

import stockPrice.StockPriceProvider;
import agent.PlayerAgent;
import exceptions.NotEnoughMoneyException;
import exceptions.NotEnoughSharesToSell;
import exceptions.WrongShareName;

public class player {

	private ShareDepositAccount aktiendepot;
	private CashAccount moneyBunker;
	private String playerName;
	private PlayerAgent agent;
	private boolean isAgentActive = false;

	// private Share bmw = new Share("BMW", 299);

	public void activateAgent() {
		isAgentActive = true;
	}

	public void deactivateAgent() {
		isAgentActive = false;
	}

	public boolean getAgentStatus() {
		return isAgentActive;
	}

	public player(String playername) {
		this.aktiendepot = new ShareDepositAccount();
		String moneyName = playername + "cash";
		this.moneyBunker = new CashAccount(500, moneyName);
		this.playerName = playername;
		
		for(Share share : StockPriceProvider.getSHARES()) {
			aktiendepot.createNew(new ShareItem(share.getAktienName()));
		}

	}

	public String getPlayerName() {
		return this.playerName;
	}

	public void buyShare(int count, float course, ShareItem item) {
		item.addShares(count, course);

	}
	
	public void addItem(String shareName) {
		this.aktiendepot.createNew(new ShareItem(shareName));
	}


	public String toString( ResourceBundle prop) {

		return this.moneyBunker.toString(prop) + "\nDepot:<br />"
				+ this.aktiendepot.toString();/* + "\n" + bmw.toString(); */

	}

	public ShareItem searchShare(String name) throws WrongShareName {

		boolean found = false;
		int getPosition = 0;
		int count = this.aktiendepot.getStockCount();

		if (this.aktiendepot.getStockCount() == 0) {
			ShareItem newItem = new ShareItem(name);
			this.aktiendepot.createNew(newItem);
		} else {

			try {
				if (this.aktiendepot.proofForShareName(name)) {
					while (!found) {
						if (this.aktiendepot.getShareName(getPosition).equals(
								name)) {
							found = true;
						} else {
							if (getPosition < count)
								getPosition++;
							else
								break;
						}

					}
				}
			} catch (WrongShareName e) {

				throw e;
			}
		}

		return this.aktiendepot.getShareItem(getPosition);

	}

	public void reduceSharesInItem(int count, ShareItem item)
			throws NotEnoughSharesToSell {
		try {
			item.reduceShares(count);
		} catch (NotEnoughSharesToSell e) {
			throw e;
		}
	}

	public float getTotalAssetValue() {
		return moneyBunker.getValue() + aktiendepot.getValue();
	}

	public Asset[] getAssets() {
		Asset[] result = new Asset[2 + aktiendepot.getStockCount()];
		result[0] = moneyBunker;
		result[1] = aktiendepot;
		for (int i = 0; i < aktiendepot.getStockCount(); i++) {
			result[1 + i] = aktiendepot.getShareItem(i);
		}

		return result;
	}

	public void reduceMoney(int count, float money)
			throws NotEnoughMoneyException {

		try {
			this.moneyBunker.reduceMoney(count, money);
		} catch (NotEnoughMoneyException e) {
			throw e;
		}
	}

	public void addMoney(int count, float money) {
		this.moneyBunker.addMoney(count, money);
	}

}
